import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  teamList: {},
  currentTeam: {},
  searchTeamCondition: {
    pageSize: 10,
    title: "",
    status: ""
  }
};

const getters = {
  teamList: state => state.teamList,
  currentTeam: state => state.currentTeam,
  searchTeamCondition: state => state.searchTeamCondition
};

const actions = {
  getTeamPageS({ commit }, param) {
    return Api.getTeamPage(param).then(ret => {
      commit(types.UPDATE_TEAM_LIST, ret.data.data);
      return ret;
    });
  },
  getTeamS({ commit }, id) {
    return Api.getTeam({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_TEAM, ret.data.data);
      return ret;
    });
  },
  addOrUpdateTeamS({ commit }, payload) {
    return Api.addOrUpdateTeam({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteTeamS({ commit }, param) {
    return Api.deleteTeam(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_TEAM](state, payload) {
    state.currentTeam = {
      duty: "",
      image: "",
      introduction: "",
      name: "",
      telphone: "",
      departId: "",
      visible: true
    };
  },
  [types.UPDATE_CURRENT_TEAM](state, payload) {
    console.log(payload);
    state.currentTeam = payload;
  },
  [types.UPDATE_TEAM_LIST](state, payload) {
    state.teamList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
